<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>SSE Example</title>
</head>
<body>
<div id="messages"></div>

<script>
         let eventSource;
        function connect() {
            eventSource = new EventSource('http://localhost:8080/sse');

            eventSource.onmessage = function(event) {
                const messagesDiv = document.getElementById('messages');
                const p = document.createElement('p');
                p.textContent = `Received message: ${event.data}`;
                messagesDiv.appendChild(p);
            };

            eventSource.onerror = function(error) {
                console.error('EventSource failed:', error);
                eventSource.close();
                setTimeout(connect, 5000); // 5 秒后重新连接
            };
        }
        connect();
    </script>
</body>
</html>